#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 2.自动求导与梯度计算.py
# @Author: Richard Chiming Xu
# @Date  : 2021/11/5
# @Desc  : Pytorch与Numpy

import torch
import numpy as np
# numpy转tensor
data_list = np.array([[1,2,3],[4,5,6],[7,8,9]])
data_tensor = torch.from_numpy(data_list)

print('数组的数据格式: {}'.format(type(data_list)))
print('张量的数据格式: {}'.format(type(data_tensor)))
print(data_tensor)

# 直接生成
x = torch.Tensor(5, 3)
y = torch.rand(5, 3)
# 相加
z = x+y
print(z)

# CPU与GPU的转换
if torch.cuda.is_available():
    x_gpu = x.cuda()
    y_gpu = y.cuda()
    z_gpu = x_gpu + y_gpu
    print(z_gpu)